package cn.lh.org.controller;

import cn.lh.basic.util.JsonResult;
import cn.lh.basic.util.Page;
import cn.lh.org.domain.Employee;
import cn.lh.org.query.EmployeeQuery;
import cn.lh.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/{id}")
    public Employee findOne(@PathVariable("id") long id) {
        return employeeService.findById(id);
    }

    @GetMapping
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") long id) {
        try {
            employeeService.del(id);
            return JsonResult.set(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "删除失败");
        }

    }

    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return JsonResult.set(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "操作失败");
        }

    }

    @PostMapping
    public Page<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryPage(query);
    }

    @PatchMapping
    public JsonResult batchDelete(@RequestBody Long[] ids){
        try {
            employeeService.batchDelete(ids);
            return JsonResult.set(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "删除失败");
        }
    }

    @GetMapping("/shopemp/{id}")
    public List<Employee> findShopEmployees(@PathVariable("id") Long shopId){

        return employeeService.findShopEmployees(shopId);
    }
}
